package profile

import (
	"context"
	"encoding/base64"
	"fmt"
	"github.com/golang/protobuf/proto"
	rentalpb "serve/rental/api/gen/v1"
	"serve/shared/id"
)

type Fetcher interface {
	GetProfile(ctx context.Context, req *rentalpb.GetProfileRequest) (*rentalpb.Profile, error)
}

type Manger struct {
	Fetcher Fetcher
}

func (m *Manger) Verify(cxt context.Context, aid id.AccountID) (id.IdentityID, error) {
	nilId := id.IdentityID("")
	p, err := m.Fetcher.GetProfile(cxt, &rentalpb.GetProfileRequest{})

	if err != nil {
		return nilId, fmt.Errorf("get Profile err %v", err)
	}

	if p.Identity_Status != rentalpb.Identity_Status_VERIFIED {
		return nilId, fmt.Errorf("invaild identity status")
	}

	marshal, err := proto.Marshal(p.Identity)
	if err != nil {
		return nilId, fmt.Errorf("cannot Marshal identity :%v", err)
	}
	return id.IdentityID(base64.StdEncoding.EncodeToString(marshal)), nil
}
